package com.hhkj.study.admin.teacher.controller;

import com.hhkj.study.admin.teacher.entity.Teacher;
import com.hhkj.study.admin.teacher.excel.TeacherExcelRes;
import com.hhkj.study.admin.teacher.model.req.TeacherReq;
import com.hhkj.study.admin.teacher.model.res.TeacherRes;
import com.hhkj.study.admin.teacher.service.biz.TeacherBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v3.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.ExcelImportBaseTemplate;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* TeacherController
*
* @author SERVER
* @since 2024-09-06
*/
@Slf4j
@RestController
@PermissionPrefix("teacher:teacher")
@Controller
@RequestMapping("/admin/teacher")
public class TeacherController extends BaseController<Teacher, TeacherReq, TeacherRes, TeacherBizService> {

    @PreAuthorize("@ss.hasPermi(this, ':export')")
    @PostMapping("/export")
    public void export(@RequestBody TeacherReq req, HttpServletResponse response) {
        try {
            getBizService().export(req, response);
        } catch (GlobalException e) {
            throw new GlobalException(e.getMessage());
        } catch (Exception e) {
            log.error("导出文件失败", e);
            throw new GlobalException("系统异常，文件导出失败，请稍后重试");
        }
    }

    /**
     * 导入
     * @return
     */
    @PreAuthorize("@ss.hasPermi(this, ':import')")
    @PostMapping("/import")
    public AjaxResult importTeacher(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return AjaxResult.success(getBizService().importTeacher(file, request));
    }

    @Override
    public ExcelImportBaseTemplate getExcelImportBaseTemplate() {
        return new TeacherExcelRes();
    }
}
